﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZedGraph; 

namespace Demonstartor
{
    public partial class MainForm : Form
    {
        bool GraphClick = false;

        FligthAbnoramalityReader ID;

        int CurAbnormality;

        public MainForm()
        {
            InitializeComponent();

            ID = new FligthAbnoramalityReader();

            CurAbnormality = -1;
        }

        private void UpdateClbColumnWidht(CheckedListBox CLB)
        {
            int ICount = CLB.Items.Count;
            int MaxLen = int.MinValue;
            for (int i = 0; i < ICount; i++)
            {
                Size CurLen = TextRenderer.MeasureText(CLB.Items[i].ToString(), CLB.Font);
                if (CurLen.Width > MaxLen)
                    MaxLen = CurLen.Width;
            }

            CLB.ColumnWidth = MaxLen;
        }


        private void button1_Click(object sender, EventArgs e)
        {

        }


        private void button3_Click(object sender, EventArgs e)
        {
            
        }

        private void btnOpenAnalysis_Click(object sender, EventArgs e)
        {
            try
            {
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    int Offset = 0;
                    if (rbnFrance.Checked)
                        Offset = -2;

                    ID.LoadAnalysis(openFileDialog1.FileName, Offset);
                    dgvAbnormalities.DataSource = ID.GetAllAbnormalities().GetDataTable();
                    clbAllDrawingParams.Items.AddRange(ID.GetAllInvolvedParams().ToArray());

                    UpdateClbColumnWidht(clbAllDrawingParams);

                    tabControl1.Enabled = true;

                    DataGridViewCellEventArgs ev = new DataGridViewCellEventArgs(0, 0);
                    dgvAbnormalities_CellClick_1(dgvAbnormalities, ev);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Ошибка во входных данных: " + Environment.NewLine + ex.Message);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void dgvInvolvedParams_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void btnSelectAll_Click(object sender, EventArgs e)
        {
            int PCount = clbDrawingParams.Items.Count;
            for (int i = 0; i < PCount; i++)
                clbDrawingParams.SetItemChecked(i, true);
        }

        private void btnUnselectAll_Click(object sender, EventArgs e)
        {
            int PCount = clbDrawingParams.Items.Count;
            for (int i = 0; i < PCount; i++)
                clbDrawingParams.SetItemChecked(i, false);
        }

        private void dgvAbnormalities_CellClick_1(object sender, DataGridViewCellEventArgs e)
        {
            int Ind = e.RowIndex;
            if ((Ind != -1) && (CurAbnormality != Ind))
            {
                CurAbnormality = Ind;

                InvolvedParametersList Params = ID.GetAllInvolvedParams(CurAbnormality, true, true);
                dgvInvolvedParams.DataSource = Params.GetDataTable();

                clbDrawingParams.Items.Clear();
                clbDrawingParams.Items.AddRange(Params.ToArray());

                UpdateClbColumnWidht(clbDrawingParams);
            }
        }

        private void btnAddParams_ChangeUICues(object sender, UICuesEventArgs e)
        {

        }

        private void btnAddParams_Click(object sender, EventArgs e)
        {
            AllParams AP = new AllParams(ID.GetAllParams());
            if (AP.ShowDialog() == DialogResult.OK)
            {
                InvolvedParametersList ResParams = AP.SelectedParams;
                int PCount = ResParams.Count();
                for (int i = 0; i < PCount; i++)
                    if (clbDrawingParams.Items.IndexOf(ResParams[i]) == -1)
                        clbDrawingParams.Items.Add(ResParams[i]);

                UpdateClbColumnWidht(clbDrawingParams);
            }
        }

        private void clbDrawingParams_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            
        }

        private void clbDrawingParams_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void btnDraw_Click(object sender, EventArgs e)
        {
            if (CurAbnormality == -1)
                return;

            InvolvedParametersList InvolvedParameters = new InvolvedParametersList(clbDrawingParams.CheckedItems.OfType<InvolvedParameter>().ToArray());

            FlightValues FV = ID.GetFlightValues(CurAbnormality, Convert.ToInt32(tbxAdditionalTime.Text), InvolvedParameters);

            if (cbxNormalize.Checked)
                Normalizer.Normalize(FV);

            FlightDrawer.Draw(FV, zgcValues);
        }

        private void clbAllDrawingParams_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void btnAllSelectAll_Click(object sender, EventArgs e)
        {
            int PCount = clbAllDrawingParams.Items.Count;
            for (int i = 0; i < PCount; i++)
                clbAllDrawingParams.SetItemChecked(i, true);
        }

        private void btnAllUnselectAll_Click(object sender, EventArgs e)
        {
            int PCount = clbAllDrawingParams.Items.Count;
            for (int i = 0; i < PCount; i++)
                clbAllDrawingParams.SetItemChecked(i, false);
        }

        private void btnDrawAll_Click(object sender, EventArgs e)
        {

            InvolvedParametersList InvolvedParameters = new InvolvedParametersList(clbAllDrawingParams.CheckedItems.OfType<InvolvedParameter>());

            FlightValues FV = ID.GetFlightValues(InvolvedParameters);
             
            if (cbxAllNormalize.Checked)
                Normalizer.Normalize(FV);

            FlightDrawer.Draw(FV, zgcAllValues);
        }

        private void btnAllAddParams_Click(object sender, EventArgs e)
        {
            AllParams AP = new AllParams(ID.GetAllParams());
            if (AP.ShowDialog() == DialogResult.OK)
            {
                InvolvedParametersList ResParams = AP.SelectedParams;
                int PCount = ResParams.Count();
                for (int i = 0; i < PCount; i++)
                    if (clbAllDrawingParams.Items.IndexOf(ResParams[i]) == -1)
                        clbAllDrawingParams.Items.Add(ResParams[i]);

                UpdateClbColumnWidht(clbAllDrawingParams);
            }
        }

        private void zgcAllValues_Load(object sender, EventArgs e)
        {

        }

        private void zgcAllValues_MouseClick(object sender, MouseEventArgs e)
        {

        }

        private bool zgcAllValues_MouseDownEvent(ZedGraphControl sender, MouseEventArgs e)
        {
            GraphClick = true;
            return default(bool);

        }

        private void zgcAllValues_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button != System.Windows.Forms.MouseButtons.None)
                GraphClick = false;
        }

        private bool zgcAllValues_MouseUpEvent(ZedGraphControl sender, MouseEventArgs e)
        {

            if (!GraphClick)
                return default(bool);

            if (e.Button != System.Windows.Forms.MouseButtons.Left)
                return default(bool);

            double x, y;
            zgcAllValues.GraphPane.ReverseTransform(e.Location, out x, out y);

            int Ind = ID.GetAbnormalityIndex((new XDate(x)).DateTime);

            if (Ind != -1)
            {
                tabControl1.SelectTab(0);
                dgvAbnormalities.CurrentCell = dgvAbnormalities.Rows[Ind].Cells[0];
                DataGridViewCellEventArgs ce = new DataGridViewCellEventArgs(0, Ind);
                dgvAbnormalities_CellClick_1(dgvAbnormalities, ce);
            }

            return default(bool);
        }

        private void zgcAllValues_ZoomEvent(ZedGraphControl sender, ZoomState oldState, ZoomState newState)
        {
        }

        private void zgcAllValues_Paint(object sender, PaintEventArgs e)
        {
            int OCount = zgcAllValues.GraphPane.GraphObjList.Count;
            for (int i = 0; i < OCount; i++)
            {
                if (zgcAllValues.GraphPane.GraphObjList[i].GetType() == typeof(ZedGraph.BoxObj))
                {
                    BoxObj Cur = (BoxObj)zgcAllValues.GraphPane.GraphObjList[i];
                    Cur.Location.Y = zgcAllValues.GraphPane.YAxis.Scale.Max;
                    Cur.Location.Height = -zgcAllValues.GraphPane.YAxis.Scale.Max + zgcAllValues.GraphPane.YAxis.Scale.Min;
                }
            }
        }

    }
}
